package com.mars.admin.modules.app;

import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.post.entity.PostCommentLike;
import com.mars.admin.modules.post.service.IPostCommentLikeService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帖子评论点赞控制器
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/api/post-comment-like")
@Tag(name = "帖子评论点赞管理", description = "帖子评论点赞相关接口")
public class PostCommentLikeController {

    @Autowired
    private IPostCommentLikeService postCommentLikeService;

    /**
     * 点赞评论
     */
    @PostMapping("/like")
    @Operation(summary = "点赞评论", description = "对评论进行点赞")
    public Result<Boolean> likeComment(
            @Parameter(description = "评论ID") @RequestParam Long commentId,
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            boolean success = postCommentLikeService.likeComment(commentId, userId);
            if (success) {
                return Result.success("点赞成功", true);
            } else {
                return Result.error("点赞失败，可能已经点赞过了");
            }
        } catch (Exception e) {
            log.error("点赞评论失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            return Result.error("点赞失败：" + e.getMessage());
        }
    }

    /**
     * 取消点赞评论
     */
    @PostMapping("/unlike")
    @Operation(summary = "取消点赞评论", description = "取消对评论的点赞")
    public Result<Boolean> unlikeComment(
            @Parameter(description = "评论ID") @RequestParam Long commentId,
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            boolean success = postCommentLikeService.unlikeComment(commentId, userId);
            if (success) {
                return Result.success("取消点赞成功", true);
            } else {
                return Result.error("取消点赞失败");
            }
        } catch (Exception e) {
            log.error("取消点赞评论失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            return Result.error("取消点赞失败：" + e.getMessage());
        }
    }

    /**
     * 检查是否点赞评论
     */
    @GetMapping("/is-liked")
    @Operation(summary = "检查是否点赞评论", description = "检查用户是否对评论点赞")
    public Result<Boolean> isLiked(
            @Parameter(description = "评论ID") @RequestParam Long commentId,
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            boolean isLiked = postCommentLikeService.isLiked(commentId, userId);
            return Result.success(isLiked);
        } catch (Exception e) {
            log.error("检查是否点赞评论失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            return Result.error("检查失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户对评论的点赞记录
     */
    @GetMapping("/user-comment-like")
    @Operation(summary = "获取用户对评论的点赞记录", description = "获取用户对指定评论的点赞记录")
    public Result<PostCommentLike> getUserCommentLike(
            @Parameter(description = "评论ID") @RequestParam Long commentId,
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            PostCommentLike commentLike = postCommentLikeService.getUserCommentLike(commentId, userId);
            return Result.success(commentLike);
        } catch (Exception e) {
            log.error("获取用户对评论的点赞记录失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 查询用户的点赞记录
     */
    @GetMapping("/user-likes")
    @Operation(summary = "查询用户的点赞记录", description = "分页查询用户的点赞记录")
    public Result<Page<PostCommentLike>> getUserLikes(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<PostCommentLike> page = new Page<>(pageNumber, pageSize);
            Page<PostCommentLike> result = postCommentLikeService.getUserLikes(userId, page);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询用户的点赞记录失败，用户ID：{}", userId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询评论的点赞记录
     */
    @GetMapping("/comment-likes")
    @Operation(summary = "查询评论的点赞记录", description = "分页查询评论的点赞记录")
    public Result<Page<PostCommentLike>> getCommentLikes(
            @Parameter(description = "评论ID") @RequestParam Long commentId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<PostCommentLike> page = new Page<>(pageNumber, pageSize);
            Page<PostCommentLike> result = postCommentLikeService.getCommentLikes(commentId, page);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询评论的点赞记录失败，评论ID：{}", commentId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 统计评论的点赞数量
     */
    @GetMapping("/count/comment")
    @Operation(summary = "统计评论的点赞数量", description = "统计指定评论的点赞数量")
    public Result<Integer> countCommentLikes(@Parameter(description = "评论ID") @RequestParam Long commentId) {
        try {
            int count = postCommentLikeService.countCommentLikes(commentId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计评论的点赞数量失败，评论ID：{}", commentId, e);
            return Result.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 统计用户的点赞数量
     */
    @GetMapping("/count/user")
    @Operation(summary = "统计用户的点赞数量", description = "统计用户的点赞数量")
    public Result<Integer> countUserLikes(@Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            int count = postCommentLikeService.countUserLikes(userId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计用户的点赞数量失败，用户ID：{}", userId, e);
            return Result.error("统计失败：" + e.getMessage());
        }
    }



    /**
     * 查询用户点赞的评论ID列表
     */
    @GetMapping("/user-liked-comment-ids")
    @Operation(summary = "查询用户点赞的评论ID列表", description = "查询用户点赞的评论ID列表")
    public Result<List<Long>> getUserLikedCommentIds(@Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            List<Long> commentIds = postCommentLikeService.getUserLikedCommentIds(userId);
            return Result.success(commentIds);
        } catch (Exception e) {
            log.error("查询用户点赞的评论ID列表失败，用户ID：{}", userId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询评论的点赞用户ID列表
     */
    @GetMapping("/comment-like-user-ids")
    @Operation(summary = "查询评论的点赞用户ID列表", description = "查询评论的点赞用户ID列表")
    public Result<List<Long>> getCommentLikeUserIds(@Parameter(description = "评论ID") @RequestParam Long commentId) {
        try {
            List<Long> userIds = postCommentLikeService.getCommentLikeUserIds(commentId);
            return Result.success(userIds);
        } catch (Exception e) {
            log.error("查询评论的点赞用户ID列表失败，评论ID：{}", commentId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据评论ID删除所有点赞记录
     */
    @DeleteMapping("/comment/{commentId}")
    @Operation(summary = "删除评论的所有点赞记录", description = "根据评论ID删除所有点赞记录")
    public Result<Boolean> deleteCommentLikes(@Parameter(description = "评论ID") @PathVariable Long commentId) {
        try {
            boolean success = postCommentLikeService.deleteCommentLikes(commentId);
            if (success) {
                return Result.success("删除成功", true);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("根据评论ID删除所有点赞记录失败，评论ID：{}", commentId, e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 根据用户ID删除所有点赞记录
     */
    @DeleteMapping("/user/{userId}")
    @Operation(summary = "删除用户的所有点赞记录", description = "根据用户ID删除所有点赞记录")
    public Result<Boolean> deleteUserLikes(@Parameter(description = "用户ID") @PathVariable Long userId) {
        try {
            boolean success = postCommentLikeService.deleteUserLikes(userId);
            if (success) {
                return Result.success("删除成功", true);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("根据用户ID删除所有点赞记录失败，用户ID：{}", userId, e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询点赞记录
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询点赞记录", description = "分页查询点赞记录")
    public Result<Page<PostCommentLike>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "评论ID") @RequestParam(required = false) Long commentId,
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId) {
        try {
            Page<PostCommentLike> page = new Page<>(pageNumber, pageSize);
            
            if (commentId != null) {
                return Result.success(postCommentLikeService.getCommentLikes(commentId, page));
            } else if (userId != null) {
                return Result.success(postCommentLikeService.getUserLikes(userId, page));
            } else {
                // 查询所有点赞记录
                Page<PostCommentLike> result = postCommentLikeService.page(page);
                return Result.success(result);
            }
        } catch (Exception e) {
            log.error("分页查询点赞记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }
} 